/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CT_Phieu_GD_DTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class CT_Phieu_GD_DAO {
    
    public static ArrayList<CT_Phieu_GD_DTO> GetALL()
    {
        ArrayList<CT_Phieu_GD_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from CT_PhieuGD" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                CT_Phieu_GD_DTO Phieu = new CT_Phieu_GD_DTO();
                Phieu.setID_GD(rs.getInt("ID_GD"));
                Phieu.setNgayGD(rs.getDate("NgayGD"));
                Phieu.setThanhTien(rs.getDouble("ThanhTien"));
                Phieu.setSoLuong(rs.getInt("SoLuong"));
                Phieu.setMaGD(rs.getString("MaGD"));
                Phieu.setMaVe(rs.getString("MaVe"));
                ds.add(Phieu);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(CT_Phieu_GD_DTO Phieu)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayGD = fmt.format(Phieu.getNgayGD());
            String sql = "Insert CT_PhieuGD(NgayGD, ThanhTien, SoLuong, MaGD, MaVe) Values('"+NgayGD+"','"+Phieu.getThanhTien()+"','"+Phieu.getSoLuong()+"','"+Phieu.getMaGD()+"','"+Phieu.getMaVe()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(CT_Phieu_GD_DTO Phieu)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayGD = fmt.format(Phieu.getNgayGD());
            String sql = "Update CT_PhieuGD Set NgayGD = '"+ NgayGD +"', ThanhTien = "+Phieu.getThanhTien()+", SoLuong = "+Phieu.getSoLuong()+", MaGD = '"+ Phieu.getMaGD()+"',MaVe = '"+Phieu.getMaVe()+"' where ID_GD = '"+Phieu.getID_GD()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from CT_PhieuGD where ID_GD = "+ maID;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    
}
